package org.abettor.util.struts2.template;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ui.AbstractUITag;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 分页标签类
 * @author shawn
 *
 */
public class PagerTag extends AbstractUITag
{
	private static final long serialVersionUID = 6344435297973921144L;
	private String action;
	private String namespace = "/";
	private String navigationLength;
	private String currentPage;
	private String totalPage;
	private String pagerVariableName;

	@Override
	public Component getBean(ValueStack stack, HttpServletRequest request, HttpServletResponse response)
	{
		super.setId("abettor_pager");
		return new PagerComponent(stack, request, response);
	}

	@Override
	protected void populateParams()
	{
		super.populateParams();
		PagerComponent pager = (PagerComponent) super.component;
		pager.setAction(action);
		pager.setNamespace(namespace);
		pager.setPagerVariableName(pagerVariableName);
		if(navigationLength.startsWith("%{") && navigationLength.endsWith("}"))
		{
			String nav = navigationLength.substring(2, navigationLength.length() - 1);
			Integer navVal = (Integer) this.getStack().findValue(nav);
			pager.setNavigationLength(navVal);
		}
		else
		{
			pager.setNavigationLength(Integer.parseInt(navigationLength));
		}
		if(currentPage.startsWith("%{") && currentPage.endsWith("}"))
		{
			String cur = currentPage.substring(2, currentPage.length() - 1);
			Integer curVal = (Integer) this.getStack().findValue(cur);
			pager.setCurrentPage(curVal);
		}
		else
		{
			pager.setCurrentPage(Integer.parseInt(currentPage));
		}
		if(totalPage.startsWith("%{") && totalPage.endsWith("}"))
		{
			String ttl = totalPage.substring(2, totalPage.length() - 1);
			Integer ttlVal = (Integer) this.getStack().findValue(ttl);
			pager.setTotalPage(ttlVal);
		}
		else
		{
			pager.setTotalPage(Integer.parseInt(totalPage));
		}
	}

	public void setAction(String action)
	{
		this.action = action;
	}

	public void setNamespace(String namespace)
	{
		this.namespace = namespace;
	}

	public void setNavigationLength(String navigationLength)
	{
		this.navigationLength = navigationLength;
	}

	public void setCurrentPage(String currentPage)
	{
		this.currentPage = currentPage;
	}

	public void setTotalPage(String totalPage)
	{
		this.totalPage = totalPage;
	}

	public void setPagerVariableName(String pagerVariableName)
	{
		this.pagerVariableName = pagerVariableName;
	}

	public String getPagerVariableName()
	{
		return pagerVariableName;
	}
}
